from django.db import models
from django.conf import settings


class BaseModel(models.Model):
    """通用基类模型，提供创建/更新时间及软删除标记。"""

    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    is_delete = models.BooleanField(default=False, verbose_name="是否删除")

    class Meta:
        abstract = True


class StudentInfo(BaseModel):
    """ 学生表 """
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name="course_student_info",
        verbose_name="关联用户",
    )
    qq = models.CharField(
        max_length=64,
        verbose_name="QQ号",
        null=True, blank=True
    )
    vx = models.CharField(
        max_length=64, 
        verbose_name="微信号", 
        null=True, blank=True
    )
    dify_user_id = models.CharField(
        max_length=64, 
        verbose_name="Dify 用户 ID", 
        null=True, blank=True
    )

    student_id = models.CharField(
        max_length=8, 
        verbose_name="学号"
    )
    student_password = models.CharField(
        max_length=64, 
        verbose_name="学号密码"
    )
    message = models.TextField(
        verbose_name="获取课表信息", 
        null=True, blank=True
    )

    class Meta:
        verbose_name = "学生"
        verbose_name_plural = "学生"

    def __str__(self):
        identifier = self.qq or self.vx or self.dify_user_id
        return f"{self.student_id}{f' - {identifier}' if identifier else ''}"


class CourseItem(BaseModel):
    """ 用户课程表 """
    student = models.ForeignKey(
        to=StudentInfo, 
        on_delete=models.CASCADE, 
        related_name="course_student", 
        verbose_name="学生"
    )
    course_id = models.CharField(
        max_length=64, 
        verbose_name="课程号", 
    )
    name = models.CharField(
        max_length=64, 
        verbose_name="课程名", 
    )
    order = models.CharField(
        max_length=32, 
        verbose_name="课序号", 
    )
    credit = models.FloatField(
        verbose_name="学分", 
        default=0
    )
    property = models.CharField(
        max_length=16, 
        verbose_name="课程属性", 
    )
    category = models.CharField(
        max_length=16, 
        verbose_name="课程类别", 
        null=True, blank=True
    )
    exam_type = models.CharField(
        max_length=16, 
        verbose_name="考试类型", 
        null=True, blank=True
    )
    teacher = models.CharField(
        max_length=64, 
        verbose_name="教师",
        null=True, blank=True
    )
    method = models.CharField(
        max_length=16, 
        verbose_name="修读方式", 
        null=True, blank=True
    )
    select_status = models.CharField(
        max_length=16, 
        verbose_name="选课状态", 
        null=True, blank=True
    )
    course_time = models.CharField(
        max_length=64, 
        verbose_name="时间", 
        null=True, blank=True
    )
    location = models.CharField(
        max_length=64, 
        verbose_name="地点", 
        null=True, blank=True
    )

    class Meta:
        verbose_name = "课表列表"
        verbose_name_plural = "课表列表"

    def __str__(self):
        return f"{self.student.student_id} 的 {self.name}"
